//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientExecuteActivate.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_current_status","_action_title","_activated_msg","_deactivated_msg","_shared_variable","_initial_status","_activated_title","_deactivated_title"
		,"_activated_code","_deactivated_code","_previous_status","_stored_act_id","_action_id","_act_id","_condition","_action_display_timer"];
DEFAULT_PARAM(_object,TMF_EXECUTE_ACTION_OBJECT,objNull);
DEFAULT_PARAM(_caller,TMF_EXECUTE_ACTION_CALLER,objNull);
DEFAULT_PARAM(_act_id,TMF_EXECUTE_ACTION_ID,-1);
DEFAULT_PARAM(_arguments,TMF_EXECUTE_ACTION_ARGS,[]);
DEFAULT_PARAM(_action,TMF_EXECUTE_ACTION_ACTION,[]);

_action_id = _arguments select TMF_EXECUTE_ACTION_PARAM_ACTID;

if (!isnull _object) then {
	if (_action_id > 0) then {
		if (count _action  <= 0) then {
			_action = [_object,_action_id] call FFUNC(actionGet);
		};
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
		_activated_msg = [_parameters, "ACTIVATION_MSG", []] call CFUNC(hashGet);
		_deactivated_msg = [_parameters, "DEACTIVATION_MSG", []] call CFUNC(hashGet);
		_shared_variable = [_parameters, "SHARED_VAR", ""] call CFUNC(hashGet);
		_initial_status = [_parameters, "SHARED_VAR_VAL", false] call CFUNC(hashGet);
		_activated_title = [_parameters, "ACTIVATED_TITLE", ""] call CFUNC(hashGet);
		_deactivated_title = [_parameters, "DEACTIVATED_TITLE", ""] call CFUNC(hashGet);
		_activated_code = [_parameters, "ACTIVATED_CODE", ""] call CFUNC(hashGet);
		_deactivated_code = [_parameters, "DEACTIVATED_CODE", ""] call CFUNC(hashGet);
		
		if (typename _activated_msg == typename "") then {_activated_msg = [_activated_msg];};
		if (typename _deactivated_msg == typename "") then {_deactivated_msg = [_deactivated_msg];};
		
		_current_status = [_object,_action,"ACTIVATE_STATUS",_initial_status] call FFUNC(actionGetLocal);
		_current_status = !_current_status;
		TRACE_1("New Status",_current_status);
		[_object,_action,"ACTIVATE_STATUS",_current_status] call FFUNC(actionSetLocal);
		
		_stored_act_id = [_object,_action,"ACTIVATE_ACTION",-1] call FFUNC(actionGetLocal);
		if (_stored_act_id >= 0) then {
			_object removeAction _stored_act_id;
			[_object,_action,"ACTIVATE_ACTION",-1] call FFUNC(actionSetLocal);
			TRACE_1("Removed Action",_stored_act_id);
		};

		_object_var = format["mission_object_activation_status_%1",_action_id];
		OSETVAR(_object,_object_var,_current_status);

		_action_title = _activated_title;
		If (! _current_status) then { _action_title = _deactivated_title; };
		// If the activation title has been left empty, it means we don't want to allow changing the state back.
		if (_action_title != "") then {
			_action_title = (_action_title) call CFUNC(translate);
			_arguments = [];
			_arguments set [TMF_EXECUTE_ACTION_PARAM_ACTID, _action_id];
			_action_display_timer = QGVAR(action_display_timer);
			_object setVariable [_action_display_timer,0];
			_condition = format ["(alive _target) && ((_target getVariable '%1')) && (_target getVariable '%2' < time)",_object_var,_action_display_timer];
			if (!_current_status) then {
				_condition = format ["(alive _target) && (!(_target getVariable '%1')) && (_target getVariable '%2' < time)",_object_var,_action_display_timer];
			};
			_act_id = _object addAction [_action_title, TMF_ADD_ACTION_SCRIPT,_arguments,10,True,True,"",_condition];
			// We set the variable as local because it is dependent on the computer running it.
			[_object,_action,"ACTIVATE_ACTION",_act_id] call FFUNC(actionSetLocal);
			TRACE_1("Added Action",ARR_4(_act_id,_action_title,typeof _object,_condition));
		};
	} else {
		ERROR("Action ID not set");
	};
} else {
	ERROR("Object not set");
};
// _object = _this select 0;
// _action = _this select 1;
// _player = _this select 3;
// _action_id = _this select 4;
// _parameters = _this select 2;
// _activated_msg = _parameters select 0;
// _deactivated_msg = _parameters select 1;
// _shared_variable = _parameters select 2;
// _activated_title = localize "STR_MF_ACTIVATE_DEFACTIONON";
// if (count _parameters > 3) then {_activated_title = _parameters select 3;};
// _deactivated_title = localize "STR_MF_ACTIVATE_DEFACTIONOFF";
// if (count _parameters > 4) then {_deactivated_title = _parameters select 4;};
// _initial_status = False;
// if (count _parameters > 5) then {_initial_status = _parameters select 5;};
// _activated_code = "";
// if (count _parameters > 6) then {_activated_code = _parameters select 6;};
// _deactivated_code = "";
// if (count _parameters > 7) then {_deactivated_code = _parameters select 7;};

// _stored_act_id = _object getVariable "mission_object_activation_action_id";
// if (isNil "_stored_act_id") then { _stored_act_id = -1 };
// if (_stored_act_id >= 0) then {_action_id = _stored_act_id};
// _object removeAction _action_id;
// _current_status = _object getVariable "mission_object_activation_status";
// if (isnil "_current_status") then {_current_status = _initial_status};
// _previous_status = _object getVariable "mission_object_activation_previous_status";
// if (isnil "_previous_status") then {_previous_status = _current_status};
// // Both previous and current status should be different. If it isn't the case, then we switch Current.
// if (!((_current_status || _previous_status) && !(_current_status && _previous_status))) then {
	// _current_status = !_current_status;
// };	
// if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: obj:%4  actm:%5 deactm:%6  var:%7  val:%8  actt:%9  deactt:%10  sts:%11  actc:%12  deactc:%13",diag_tickTime,"mission_functions.sqf","XfMissionObjectClientExecuteActivate",typeof _object, _activated_msg, _deactivated_msg, _shared_variable, _shared_variable_value, _activated_title, _deactivated_title, _current_status, (_activated_code != ""),(_deactivated_code != "")];};
// If (isNil "_current_status") then {
	// _current_status = _initial_status;
	// _current_status = !_current_status;
// };
// _object setVariable ["mission_object_activation_previous_status",_current_status];

// if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: obj:%4  actm:%5 deactm:%6  var:%7  val:%8  actt:%9  deactt:%10  sts:%11  actc:%12  deactc:%13",diag_tickTime,"mission_functions.sqf","XfMissionObjectClientExecuteActivate",typeof _object, _activated_msg, _deactivated_msg, _shared_variable, _shared_variable_value, _activated_title, _deactivated_title, _current_status, (_activated_code != ""),(_deactivated_code != "")];};
// _action_title = _activated_title;
// If (! _current_status) then { _action_title = _deactivated_title; };
// // If the activation title has been left empty, it means we don't want to allow changing the state back.
// if (_action_title != "") then {
	// _action_id = _object getVariable "mission_object_activation_action";
	// if !(isnil "_action_id") then { _object removeAction _action_id };
	// _file_name = format["%1process_action.sqf",d_mission_base_path];
	// _action_id = _object addAction [_action_title, _file_name, [],10,True,True,"","(alive _target)"];
	// // We set the variable as local because it is dependent on the computer running it.
	// _object setVariable ["mission_object_activation_action",_action_id];
// };

TRACE("End");
TMFPROFILERSTOP;